/* eslint-disable @typescript-eslint/no-explicit-any */
export enum EventEnum {
  TABLE_RELOAD,
  DETAIL_RELOAD,
}
export type IEmits = {
  on: (event: EventEnum, callback: (data?: any) => any) => void,
  off: (event: EventEnum) => void,
  trigger: (event: EventEnum, data?: any) => void,
  triggerOwner: (event: EventEnum, data?: any) => void,
}
const emits = {
  _pool: [] as { event: string, callback: (data?: any) => any }[],
  on(event: EventEnum, callback: (data?: any) => any) {
    this._pool.push({ event: event.toString(), callback });
  },
  off(event: EventEnum) {
    const indexs = [] as number[];
    this._pool.forEach((x, index) => {
      if (x.event === event.toString()) {
        indexs.push(index);
      }
    });
    indexs.forEach(x => {
      this._pool.splice(x, 1);
    })

  },
  trigger(event: EventEnum, data?: any) {
    this._pool.filter(x => x.event === event.toString()).forEach(e => {
      if (e && typeof e.callback === "function") {
        e.callback(data);
      }
    })
  },
  triggerOwner(event: EventEnum, data?: any) {
    if (window.opener && window.opener.emits && typeof window.opener.emits.trigger === "function") {
      window.opener.emits.trigger(event, data);
    } else {
      this.trigger(event, data);
    }
  }
}
window.emits = emits;
export default emits;
